package com.woniuxy.yuekeuser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.dao.IncomeMapper;
import com.woniuxy.yuekeuser.dao.OrderMapper;
import com.woniuxy.yuekeuser.info.UserInfo;
import com.woniuxy.yuekeuser.model.Income;
import com.woniuxy.yuekeuser.model.Order;
import com.woniuxy.yuekeuser.model.User;
import com.woniuxy.yuekeuser.dao.UserMapper;
import com.woniuxy.yuekeuser.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.NullArgumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
     @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IncomeMapper incomeMapper;
    @Override
    public String modifyMassage(User user) {

        log.info("userSevice修改user信息{}",user);
        userMapper.updateById(user);

        return "修改执行成功";
    }

    @Override
    public IPage<Order> getUserOrder(Page<Order> page, int userId, String state) {
        log.info("参数为：{}",page);
        IPage<Order> p=orderMapper.getUserOrder(userId,state,page);
        return p;
    }

    @Override
    public IPage<Income> getUserIncome(Page<Income> page, int userId) {
        IPage<Income> iPage=incomeMapper.getUserIncome(page,userId);
        return iPage;
    }




    /**
     * @MethodName userRegister
     * @Description 用户注册
     * @Author Cheney
     * @Date 2020/11/3 14:35
     **/
    @Override
    public boolean userRegister(User user) {
        log.info("用户数据为：{}",user);

        if(user==null){
            throw new NullArgumentException("用户数据不能为空！");
        }

        // 查询该用户是否被注册
        User user2 = userMapper.selectByUserTel(user.getUserTel());

        System.out.println(user2);

        if(user2 == null){
            userMapper.insert(user);

            userMapper.insertRole(user.getUserId());

            return true;
        }else{
            return false;
        }
    }

    /**
     * @MethodName userLogin
     * @Description 用户登录
     * @Author Cheney
     * @Date 2020/11/3 14:35
     **/
    @Override
    public UserInfo userLogin(String userTel, String password) {

        return userMapper.selectByUserTelAndPasswordAndPassword(userTel,password);
    }

    @Override
    public UserInfo userLoginSMS(String tel) {
        return userMapper.selectByTel(tel);
    }

    @Override
    public void updatamessige(User user) {
        userMapper.updatamessige(user);
    }

    @Override
    public User findUserByUserId(int userId) {
        return userMapper.findUserByUserId(userId);
    }
}
